package com.ht.right.action;


import org.apache.struts2.ServletActionContext;

import com.ht.right.entity.Dept;
import com.ht.right.service.DeptService;
import com.ht.right.util.CommonUtil;
import com.opensymphony.xwork2.ActionSupport;

public class DeptAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private DeptService deptService;
	private Dept dept;

	public String findDeptById() {
		String deptId = ServletActionContext.getRequest().getParameter("deptId");
		dept = deptService.findDeptById(deptId);
		return "success";
	}

	// 获取所有的子部门json
	public String findChildDeptsJson() {
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),
				deptService.findChildDeptsJson(dept));
		return null;

	}

	// 获取所有的部门json
	public String findAllDeptsJson() {
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),deptService.findAllDeptsJson());
		return null;
	}

	public String deleteDept() {
		return null;
	}

	public String addDept() {
		String sameLevelNodeId = ServletActionContext.getRequest()
				.getParameter("sameLevelNodeId");
		String html = null;
		try {
			deptService.addDept(dept, sameLevelNodeId);
			html = "<script>parent.LoadOrgTree();parent.goCloseDlg(\"oWinDlg\");</script>";
		} catch (Exception e) {
			html = "<script>alert(\"添加失败\");parent.goCloseDlg(\"oWinDlg\");</script>";
			e.printStackTrace();
		}
		CommonUtil.toString(ServletActionContext.getResponse(), html);
		return null;
	}

	public String updateDept() {
		String html = null;
		String sameLevelNodeId = ServletActionContext.getRequest()
				.getParameter("sameLevelNodeId");
		try {
			deptService.updateDept(dept, sameLevelNodeId);
			html = "<script>parent.LoadOrgTree();parent.goCloseDlg(\"oWinDlg\");</script>";
		} catch (Exception e) {
			html = "<script>alert(\"保存失败\");parent.goCloseDlg(\"oWinDlg\");</script>";
			e.printStackTrace();
		}
		CommonUtil.toString(ServletActionContext.getResponse(), html);
		return null;

	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
}
